package com.probiz.estore.exports.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import com.probiz.estore.core.util.ExcelUtils;
import com.probiz.estore.exports.handler.ColumnHandler;
import com.probiz.estore.exports.model.Column;
import com.probiz.estore.exports.model.ExportMappingConfig;
import com.probiz.estore.exports.service.BaseExportManager;

public class BaseExportManagerImpl implements BaseExportManager {

	protected ExportMappingConfig exportMappingConfig;
	
	public ExportMappingConfig getExportMappingConfig() {
		return exportMappingConfig;
	}
	
	protected BaseExportManager exportManager;
	
	public void setExportManager(BaseExportManager exportManager) {
		this.exportManager = exportManager;
	}	
	
	public void setExportMappingConfig(ExportMappingConfig exportMappingConfig) {
		this.exportMappingConfig = exportMappingConfig;
	}
	/**
	 * 获取要导出的列信息
	 * @param list
	 * @return
	 */
	public List<Column> getColumns(List<Object> list){
		return exportMappingConfig.getColumns();
	}
	/**
	 * 导出主要方法
	 * @param list
	 * @param workbook
	 */
	public void buildExcel(List<Object> list, HSSFWorkbook workbook){
		List<Column> columns = exportMappingConfig.getColumns();
		List<String> listHeader = returnHeader(columns);
		HSSFSheet sheet = workbook.createSheet();
		Integer rowIdx = 1;
		for (Object target : list) {
			boolean son = ifExistChildren(target);
			if(son){
				rowIdx = buildExcelChildren(target, sheet, columns, listHeader, rowIdx);
			} else {
				HSSFRow row = sheet.createRow(rowIdx);
				for (Column column : columns) {
					ColumnHandler handler = column.getHandler();
					String attrValue = handler.getProperty(target, column, sheet, row, listHeader);
					int cell = columns.indexOf(column);
					if(StringUtils.isNotBlank(attrValue)){
						ExcelUtils.setCellStringValue(row,(short)cell,attrValue);
					}
				}
				rowIdx++ ;
			}
			
		}
		writeHeader(sheet, listHeader);
	}
	/**
	 * 判断是否有子类 (例如：产品的多个sku)
	 * @param target
	 * @return
	 */
	public boolean ifExistChildren(Object target){
		return false;
	}
	/**
	 * 有子类的导出实现
	 * @param target
	 * @param sheet
	 * @param columns
	 * @param listHeader
	 * @param rowIdx
	 * @return
	 */
	public Integer buildExcelChildren(Object target, HSSFSheet sheet,
			List<Column> columns, List<String> listHeader, Integer rowIdx) {
		return null;
	}
	
	/**
	 * 生成列头
	 * @param sheet
	 */
	private void writeHeader(HSSFSheet sheet, List<String> listHeader) {
		HSSFRow row = sheet.createRow((short)0);
		for (int i = 0; i < listHeader.size(); i++) {
			if(StringUtils.isNotBlank(listHeader.get(i))){
				ExcelUtils.setCellStringValue(row,(short)i,listHeader.get(i));
			}
		}
	}
	/**
	 * 获得列头
	 * @param columnHead
	 * @return
	 */
	private List<String> returnHeader(List<Column> columnHead){
		List<String> list =new ArrayList<String>();
		for (Column column : columnHead) {
			if(StringUtils.isNotBlank(column.getColumnHeader())){
				list.add(column.getColumnHeader());
			}
		}
		return list;
	}
	/**
	 * 获取要导出的源数据
	 * @param ids
	 * @return
	 */
	public List<Object> getDataSource(String[] ids) {
		
		return null;
	}


}
